package com.lfnyxy.web;


import com.lfnyxy.common.LogTool;
import com.lfnyxy.common.dto.ApiResult;
import com.lfnyxy.common.dto.CodeMessage;
import com.lfnyxy.entity.FileEntity;
import com.lfnyxy.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:XXX
 * @Description:
 * @Created by wanghaisheng on 2017/7/15.
 * Modified By:
 */
@Controller
@RequestMapping("/fileUpload")
public class FileUploadController {

    @Autowired
    FileUploadService fileUploadService;

    @RequestMapping(value = "/up", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult<FileEntity> uploadFile1(@RequestParam(required = false) MultipartFile file ) {
        FileEntity fileUpload = null;
        try {
            if (file != null){
                fileUpload = fileUploadService.fileUpload(file);
            }else{
                return ApiResult.error(CodeMessage.SAVE_FAILED);
            }

        }catch (Exception ex) {
            LogTool.WriteError(this.getClass().toString() + "001", ex.getMessage());
            return ApiResult.error(CodeMessage.SAVE_FAILED);
        }
        return ApiResult.success(fileUpload);
    }
}
